package edu.lambdateacher.awt;

/*
 LambdaTeacher
 Copyright (C) 2000  Kim Mason
 Copyright (C) 2014  Vitalij Zadneprovskij

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.awt.Button;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * MessageDialog is simply a modal dialog which displays a string.
 */
public class MessageDialog extends Dialog {

	private static final long serialVersionUID = -5578049461328667400L;

	Button okbutton;
	ActionListener okbuttonactionlistener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			MessageDialog.this.setVisible(false);
		}
	};

	KeyAdapter keylistener = new KeyAdapter() {
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				MessageDialog.this.setVisible(false);
			}
		}
	};

	TextArea thearea;

	public MessageDialog(Frame parent, String windowstring, String messagestring) {
		// create a modal dialog
		super(parent, windowstring, true);
		// add a handler for key hits
		this.addKeyListener(keylistener);
		okbutton = new Button("OK");
		okbutton.addActionListener(okbuttonactionlistener);
		okbutton.addKeyListener(keylistener);
		// create a textarea with the string, with only vertical scrollbars
		thearea = new TextArea(messagestring, 15, 50, TextArea.SCROLLBARS_VERTICAL_ONLY);
		thearea.setEditable(false);
		thearea.addKeyListener(keylistener);

		this.add("Center", thearea);
		this.add("South", okbutton);
		this.pack();
	}

}